package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import Entity.ManufacturesDBEntity;
import Entity.ManufacturyEntity;
import android.util.Xml;


public class XmlManufacturesParser {
	 private static final String ns = null;
	 public ArrayList<ManufacturesDBEntity> parse(InputStream in) throws XmlPullParserException, IOException {   
		 try {
         XmlPullParser parser = Xml.newPullParser();
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);       
         parser.setInput(in , null);
         parser.nextTag();       
         return readManufacturesXml(parser); 
	 } finally {
         in.close();
     }
 }
	 
	 private ArrayList<ManufacturesDBEntity> readManufacturesXml(XmlPullParser parser) throws XmlPullParserException, IOException {
		 parser.require(XmlPullParser.START_TAG, ns, "data");
		 ArrayList<ManufacturesDBEntity> manufactures = null;
		 while (parser.next() != XmlPullParser.END_TAG) {
			 if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
			 String name = parser.getName();
			 if (name.equals("manufactures")) {
				 manufactures = readManufactures(parser) ;
				 
			 }
	            
	            else{
	            	XmlTemplateParser.skip(parser);
	            }
		 }
		 return manufactures;
	 }
	 
	 private ArrayList<ManufacturesDBEntity> readManufactures(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<ManufacturesDBEntity> list= new ArrayList<ManufacturesDBEntity>();
	        parser.require(XmlPullParser.START_TAG, ns, "manufactures");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("manufacture")) {
	            	
	            	 list.add(readManufacture(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }

	        return list;
	    }
	 
	 private ManufacturesDBEntity readManufacture (XmlPullParser parser)throws IOException, XmlPullParserException {
		 ManufacturesDBEntity mEntity = new ManufacturesDBEntity() ; 	
		 mEntity.setManufactureName(parser.getAttributeValue(null, "name"));
		 ArrayList<MyFifthSet> list= new ArrayList<MyFifthSet>();
		  parser.require(XmlPullParser.START_TAG, ns, "manufacture");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("manus")) {
	            	
	            	list.add(readManus(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }
	        mEntity.setManus(list);
		 return  mEntity;
	 }
	 
 
	 private OrderedFifthSet<String, String, String,String,String > readManus (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value1 = null ; 
		 String value2 = null ; 
		 String value3 = null ; 
		 String value4 = null ; 
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "manuname");
	      key= XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "manuname");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "manucode");     
	        value1 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "manucode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "manuunit");     
	        value2 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "manuunit");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "manuunitcode");     
	        value3 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "manuunitcode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "manuprice");     
	        value4 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "manuprice");
	        parser.next();
	        parser.next();
	        OrderedFifthSet <String, String, String , String , String> manus = new  OrderedFifthSet<String, String, String ,String ,String>(key,value1,value2,value3,value4);
		 return  manus;
	 } 
	 
	 
	  
	
}
